{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "import zmq\n",
    "from zmq.asyncio import Context\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "router_url='tcp://127.0.0.1:15555'\n",
    "dealer_url='tcp://*:15555'\n",
    "url2='tcp://192.168.1.112:5555'\n",
    "#loop=asyncio.get_event_loop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 客户端"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connecting to hello world server…\n",
      "Sending request 0 …\n",
      "Received reply 0 [ b'World' ]\n",
      "Sending request 1 …\n",
      "Received reply 1 [ b'World' ]\n",
      "Sending request 2 …\n",
      "Received reply 2 [ b'World' ]\n",
      "Sending request 3 …\n",
      "Received reply 3 [ b'World' ]\n",
      "Sending request 4 …\n",
      "Received reply 4 [ b'World' ]\n",
      "Sending request 5 …\n",
      "Received reply 5 [ b'World' ]\n",
      "Sending request 6 …\n",
      "Received reply 6 [ b'World' ]\n",
      "Sending request 7 …\n",
      "Received reply 7 [ b'World' ]\n",
      "Sending request 8 …\n",
      "Received reply 8 [ b'World' ]\n",
      "Sending request 9 …\n",
      "Received reply 9 [ b'World' ]\n"
     ]
    }
   ],
   "source": [
    "import zmq\n",
    "import threading\n",
    "def client():\n",
    "    context_c = zmq.Context()\n",
    "\n",
    "    #  Socket to talk to server\n",
    "    print(\"Connecting to hello world server…\")\n",
    "    socket = context_c.socket(zmq.REQ)\n",
    "    socket.connect(url2)\n",
    "\n",
    "    #  Do 10 requests, waiting each time for a response\n",
    "    for request in range(10):\n",
    "        print(\"Sending request %s …\" % request)\n",
    "        socket.send(b\"Hello\")\n",
    "\n",
    "        #  Get the reply.\n",
    "        message = socket.recv()\n",
    "        print(\"Received reply %s [ %s ]\" % (request, message))\n",
    "\n",
    "t = threading.Thread (target=client)\n",
    "t.start()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 服务端"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Task pending coro=<server() running at <ipython-input-4-e1c165b01d9b>:8>>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n",
      "Received request: b'Hello'\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import zmq\n",
    "import asyncio\n",
    "context = zmq.Context()\n",
    "socket = context.socket(zmq.REP)\n",
    "socket.bind(\"tcp://*:5555\")\n",
    "\n",
    "async def server():\n",
    "    while True:\n",
    "        #  Wait for next request from client\n",
    "        message = socket.recv()\n",
    "        print(\"Received request: %s\" % message)\n",
    "\n",
    "        #  Do some 'work'\n",
    "        time.sleep(1)\n",
    "\n",
    "        #  Send reply back to client\n",
    "        socket.send(b\"World\")\n",
    "loop=asyncio.get_event_loop()\n",
    "loop.create_task(server())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Task pending coro=<server() running at <ipython-input-1-9f5d9c8663b9>:9>>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# router "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#client\n",
    "import zmq\n",
    "\n",
    "#  Prepare our context and sockets\n",
    "context = zmq.Context()\n",
    "socket = context.socket(zmq.REQ)\n",
    "socket.connect(\"tcp://localhost:5559\")\n",
    "\n",
    "#  Do 10 requests, waiting each time for a response\n",
    "for request in range(1,11):\n",
    "    socket.send(b\"Hello\")\n",
    "    message = socket.recv()\n",
    "    print(\"Received reply %s [%s]\" % (request, message))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## router -dealer pyxoy\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import zmq\n",
    "\n",
    "# Prepare our context and sockets\n",
    "context = zmq.Context()\n",
    "frontend = context.socket(zmq.ROUTER)\n",
    "backend = context.socket(zmq.DEALER)\n",
    "frontend.bind(\"tcp://*:5559\")\n",
    "backend.bind(\"tcp://*:5560\")\n",
    "\n",
    "# Initialize poll set\n",
    "poller = zmq.Poller()\n",
    "poller.register(frontend, zmq.POLLIN)\n",
    "poller.register(backend, zmq.POLLIN)\n",
    "\n",
    "# Switch messages between sockets\n",
    "while True:\n",
    "    socks = dict(poller.poll())\n",
    "\n",
    "    if socks.get(frontend) == zmq.POLLIN:\n",
    "        message = frontend.recv_multipart()\n",
    "        backend.send_multipart(message)\n",
    "\n",
    "    if socks.get(backend) == zmq.POLLIN:\n",
    "        message = backend.recv_multipart()\n",
    "        frontend.send_multipart(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 问题\n",
    "jupyter 使用zmq 加入task 出问题\n",
    "问题出在bing 值类 有阻塞问题"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
